package com.whmall.service.crm.module.invoice.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.service.crm.entity.dto.invoice.*;
import com.whmall.service.crm.module.invoice.dao.SettleCollegeAccountMapper;
import com.whmall.service.crm.module.invoice.entity.SettleCollegeAccountDO;
import com.whmall.service.crm.module.invoice.service.SettleCollegeAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SettleCollegeAccountServiceImpl implements SettleCollegeAccountService {

    @Autowired
    private SettleCollegeAccountMapper accountMapper;

    @Autowired
    private SettleCollegeAccountValidator validator;

    @Override
    public PageRspDTO<SettleCollegeAccountRspDTO> query(SettleCollegeAccountQueryReqDTO reqDTO, PageReqDTO pageReqDTO) {

        Assert.notNull(reqDTO, "参数异常");
        SettleCollegeAccountQueryReqDTO queryReqDTO = validator.validateQuery(reqDTO);
        //按照创建时间排序
        queryReqDTO.setOrderBy("create_time desc");
        PageInfo<SettleCollegeAccountDO> pageInfo = PageHelper.startPage(pageReqDTO.getPageNum(),
                pageReqDTO.getPageSize()).doSelectPageInfo(() -> accountMapper.selectBy(queryReqDTO));
        List<SettleCollegeAccountRspDTO> settleCollegeAccountRspDTOS = new ArrayList<>();
        if (CollUtil.isEmpty(pageInfo.getList())){
            return new PageRspDTO<>(pageInfo.getPageNum(), pageInfo.getPageSize(),
                    pageInfo.getPages(), (int) pageInfo.getTotal(), settleCollegeAccountRspDTOS);
        }
        pageInfo.getList().forEach(settleCollegeAccountDO -> {
            SettleCollegeAccountRspDTO settleCollegeAccountRspDTO = new SettleCollegeAccountRspDTO();
            BeanUtils.copyProperties(settleCollegeAccountDO, settleCollegeAccountRspDTO);
            settleCollegeAccountRspDTOS.add(settleCollegeAccountRspDTO);
        });
        return new PageRspDTO<>(pageInfo.getPageNum(), pageInfo.getPageSize(),
                pageInfo.getPages(), (int) pageInfo.getTotal(), settleCollegeAccountRspDTOS);
    }

    @Override
    @Transactional
    public Long create(SettleCollegeAccountAddReqDTO reqDTO) {

        validator.validateAdd(reqDTO);
        SettleCollegeAccountDO settleCollegeAccountDO = new SettleCollegeAccountDO();
        BeanUtils.copyProperties(reqDTO, settleCollegeAccountDO);

        settleCollegeAccountDO.setCreateId(reqDTO.getEmployeeId());
        settleCollegeAccountDO.setCreateTime(new Date());
        accountMapper.insertSelective(settleCollegeAccountDO);
        return settleCollegeAccountDO.getId();
    }

    @Override
    @Transactional
    public void update(SettleCollegeAccountUpdateReqDTO reqDTO) {

        validator.validateUpdate(reqDTO);
        SettleCollegeAccountDO settleCollegeAccountDO = new SettleCollegeAccountDO();
        BeanUtils.copyProperties(reqDTO, settleCollegeAccountDO);

        settleCollegeAccountDO.setUpdateId(reqDTO.getEmployeeId());
        settleCollegeAccountDO.setUpdateTime(new Date());
        accountMapper.updateByIdSelective(settleCollegeAccountDO);
    }
}
